/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.smsspamreport;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 
 * <p>This activity prompt a user if he want to report a suspect sms to the 33700 Service</p>
 * 
 *  <p>It will retrieve the text and the phone number of the sms in the extra of the intent with the keys<br/>
 *  <ul>
 *  <li>DisplayArlertActivity.SMS_BODY for the message.</li>
 *  <li>DisplayArlertActivity.SMS_SENDER for the phone number.</li>
 *  </ul></p>
 *  <p>If the user decide to singal the message, the content will be forwarded by SMS to the 33700 and the SMS send stored in the SMS sent box.<br/>
 *  (N.B., this might not be working if the user is not using the standard sms application)</p>
 * 
 * @author Christophe Petit
 *
 */
public class DisplayArlertActivity extends Activity implements OnClickListener{

	public static final String SMS_BODY="com.smsspamreport.sms";
	public static final String SMS_SENDER="com.smsspamreport.sender";
	String sms="";
	String emeteur="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sms=getIntent().getStringExtra(SMS_BODY);
		emeteur=getIntent().getStringExtra(SMS_SENDER);
		((TextView) findViewById(R.id.main_sms)).setText("Message du:"+emeteur+"\n\n"+sms);
		findViewById(R.id.main_annuller).setOnClickListener(this);
		findViewById(R.id.main_signaler).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.main_annuller:
			finish();
			break;
		case R.id.main_signaler:
			signalSMS();
			finish();
			break;
		default:return;

		}

	}

	private void signalSMS() {
		Editor editor=getSharedPreferences("spamNumber", Context.MODE_PRIVATE).edit();
		editor.clear();
		editor.putString(SMSFilter.SPAMNUMBER,emeteur);
		editor.commit();

		SmsManager smsManager=SmsManager.getDefault();
		smsManager.sendTextMessage("33700", null, sms, null, null);
		//On sauvegarde le sms emis dans la boite sent, que l'utilisateur
		ContentValues values = new ContentValues();
		values.put("address", "33700");
		values.put("body", sms);
		getContentResolver().insert(Uri.parse("content://sms/sent"), values);
	}

}
